import React, { Component } from 'react'
import { Layout } from 'antd';
import {Redirect,Switch,Route} from 'react-router-dom'
import memoryUtils from '../../utils/memoryUtils'

import Header from '../../components/header/index'
import LeftNav from '../../components/left_nav/index'

import Home from '../home/home'
import Category from '../category/category'
import Product from '../product/product'
import Role from '../role/role'
import User from '../user/user'
import Bar from '../charts/bar'
import Line from '../charts/line'
import Pie from '../charts/pie'
import NotFound from '../not-found/not-found'

const { Footer,Sider,Content } = Layout;

export default class Admin extends Component {
    render() {
      // 获取用户信息【方式一】
      // const user = JSON.parse(localStorage.getItem('user_key') || '{}')
      // 获取用户信息【方式二】
      const user = memoryUtils.user
        if(!user._id){
            return <Redirect to="/login"/>
        }

        return (
            <Layout style={{height:'100%'}}>
            <Sider>
                <LeftNav></LeftNav>
            </Sider>
            <Layout>
              <Header></Header>
              <Content style={{backgroundColor:'#fff', margin:'30px'}}>
                {/* 这个是二级路由，需要写在admin内部 */}
                <Switch>
                  {/* 如果访问的是/，直接跳转到/admin/home中， 需要完全匹配才跳转 */}
                  <Redirect exact from='/admin' to='/admin/home'/>
                  <Route path="/admin/home" component={Home}/>
                  <Route path='/admin/category' component={Category}/>
                  <Route path='/admin/product' component={Product}/>
                  <Route path='/admin/role' component={Role}/>
                  <Route path='/admin/user' component={User}/>
                  <Route path='/admin/charts/bar' component={Bar}/>
                  <Route path='/admin/charts/line' component={Line}/>
                  <Route path='/admin/charts/pie' component={Pie}/>
                  <Route component={NotFound} />>
                </Switch>
              </Content>
              <Footer style={{textAlign:'center'}}>欢迎使用Google浏览器</Footer>
            </Layout>
          </Layout>
        )
    }
}


/***
 * 404 页面
 * 1. 如果访问的是/admin，直接跳转到/admin/home中， 需要完全匹配才跳转【先写重定向】
 <Redirect exact from='/admin' to='/admin/home'/>
 
 *2. 写 如果不匹配路径就显示 404 页面
 *<Route component={NotFound} />>
 */
